# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
         self.val = val
         self.next = next
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        cnt = 0
        result = 0
        h1 = l1
        h2 = l2
        signal = 0
        while True:
            v = h1.val + h2.val + signal
            if v >= 10:
                h1.val = v - 10
                signal = 1
            else:
                h1.val = v
                signal = 0
            if (h1.next is None) or (h2.next is None):
                break
            else:
                h1 = h1.next
                h2 = h2.next

        if h2.next is not None:
            h1.next = h2.next

        while (signal == 1) and (h1.next is not None):
            h1 = h1.next
            v = h1.val + signal
            if v >= 10:
                h1.val, signal = 0, 1
            else:
                h1.val, signal = v, 0

        if signal == 1:
            h1.next = ListNode(1)

        return l1